package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.database.LocalCache;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import surreal.triviamaster.utilities.Utilities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class FlagQuestion extends Activity{

	@Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.flag_question);
	    
	    Button submit = (Button) findViewById(R.id.submit);
	    submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(Utilities.isValidToUpload(((TextView) findViewById(R.id.other)).getText().toString())){ 
	            	submitFlagQuestion();
	            	
	            	Intent intent = new Intent(FlagQuestion.this, Game.class);
		  	      	startActivity(intent);
            	} else {
            		Toast.makeText(FlagQuestion.this, TriviaMasterApplication.ERROR_INVALID_CHARACTERS, 10000).show();
            	}
            }

			private void submitFlagQuestion() {
				//these two represent boolean values to be placed into local cache
            	int inappropriate = 0;
            	int incomplete =0;
            	if(((CheckBox) findViewById(R.id.inappropriate)).isChecked()) {
            		inappropriate=1;
            	}
            	if(((CheckBox) findViewById(R.id.incomplete)).isChecked()) {
            		incomplete=1;
            	}
				
				//send flag to local cache
        	    LocalCache db = ((TriviaMasterApplication) getApplication()).getLocalCache();
        	    db.flagCurrentQuestion(0, 0, inappropriate,incomplete,"", ((TextView) findViewById(R.id.other)).getText().toString());
				
			}
        });
	    
	    Button cancel = (Button) findViewById(R.id.cancel);
	    cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	Intent intent = new Intent(FlagQuestion.this, Game.class);
            	//decreasing index by one, because it auto increase on the game page
            	LocalCache db= ((TriviaMasterApplication) getApplication()).getLocalCache();
				db.decrementUserQuizIndex();
	  	      	startActivity(intent);
            }
        });
	}
}
